import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getPosIDs, getUserDepID, getUserID, requestFunComponFiex, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { useRequest } from 'umi';
import moment from 'moment';

let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang'; // 本地mock

/**
 * 获取资产报废详情
 * @param getDetailCallback
 * @returns
 */
const getDetailService = (getDetailCallback: (res: any) => void) => {
  const getDetail = useRequest(
    (id: any) => {
      let personUrl = `${baseUrl}/assetApplyHandle/detail/${id}/${getUserID()}`;
      const body = {
        id: id,
      };

      return requestFunComponFiex('GET', personUrl, body);
    },
    {
      manual: true,
      onSuccess: (res) => {
        getDetailCallback(res);
      },
    },
  );
  return {
    getDetailRun: getDetail?.run,
    getDetailLoading: getDetail?.loading,
  };
};

/**
 * 新增资产报废
 * @param addStorageCallback
 * @returns
 */
const addStorageService = (addStorageCallback: (res: any) => void) => {
  const addStorage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetApplyHandle/add`;
      let bodyParams = {
        ...params,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res) => {
        addStorageCallback(res);
      },
    },
  );

  return {
    addStorageRun: addStorage?.run,
    addStorageLoading: addStorage?.loading,
  };
};

/**
 * 编辑资产报废
 * @param editStorageCallback
 * @returns
 */
const editStorageService = (editStorageCallback: (res: any) => void) => {
  const editStorage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetApplyHandle/edit`;
      let bodyParams = {
        ...params,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res) => {
        editStorageCallback(res);
      },
    },
  );

  return {
    editStorageRun: editStorage?.run,
    editStorageLoading: editStorage?.loading,
  };
};

/** 资产报废审批通过后调用 */
const approvedAssetDiscardSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/assetApplyHandle/approved`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    approvedAssetDiscardUseRun: request?.run,
    approvedAssetDiscardUseLoading: request?.loading,
  };
};

/**
 * 删除申请
 * @param callback 接口回调
 * @returns
 */
const deleteApplyManageService = (callback: (res: any, clickType: boolean) => void) => {
  const deleteApplyManage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetApplyHandle/cancel`;
      let bodyParams = {
        ...params,
        cancelerID: getUserID(),
        cancelPositionID: getPosIDs(),
        cancelDepartmentID: getUserDepID(),
        cancelCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    deleteApplyManageRun: deleteApplyManage?.run,
    deleteApplyManageLoading: deleteApplyManage?.loading,
  };
};

export { addStorageService, editStorageService, getDetailService, approvedAssetDiscardSerivice, deleteApplyManageService };
